//cookie
function Cookie() { };
Cookie.prototype.set = function (key, value, expires) {
    var data = new Date();
    data.setSeconds(data.getSeconds() + expires);
    document.cookie = key + '=' + value + ';expires=' + data.toUTCString();
}
Cookie.prototype.get = function (key) {
    var cookie = document.cookie;
    var arr = cookie.split('; ');
    var obj = {};
    arr.forEach(item => {
        var str = item.replace(/=/, '*a*')
        var arr = str.split('*a*')
        obj[arr[0]] = arr[1]
    })
    return obj[key]
}
Cookie.prototype.remove = function (key) {
    this.set(key, '', -1)
}
//搜索
$('.search_btn').click(function () {
    var search = $('.head_search > input').val();
    var c = new Cookie();
    c.set('q', search)
    location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/Product.li.html#q=' + search;
    $('.head_search > input').val('');
});
document.addEventListener('keydown', function (e) {
    if (e.key == 'Enter') {
        var search = $('.head_search > input').val();
        var c = new Cookie();
        c.set('q', search)
        location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/Product.li.html#q=' + search;
        $('.head_search > input').val('');
    }
})
//编辑，添加个人信息
//添加
$('.add_shxx span').click(function () {
    $('.shxx_set').css('display', 'block').siblings().css('display', 'none')
})
//删除
var tbody = document.querySelector('tbody')
var btn_del = document.querySelectorAll('.btn-danger')
for (var i = 0; i < btn_del.length; i++) {
    btn_del[i].onclick = function () {
        tbody.removeChild(this.parentNode.parentNode)
    }
}
//编辑
$('.btn-success').click(function () {
    $('.shxx_set').css('display', 'block').siblings().css('display', 'none')
})
// 返回
$('#set_no').click(function () {
    $('.set_name > input').val(' ')
    $('.set_iphone > input').val(' ')
    $('.set_address > input').val(' ')
    sf.innerHTML = "<option value='0' disabled selected>请选择省份</option>"
    sq.innerHTML = "<option value='0' disabled selected>请选择市区</option>"
    xc.innerHTML = "<option value='0' disabled selected>请选择县城</option>"
    $('.shxx_set').css('display', 'none').siblings().css('display', 'block')
})
//省市联动
var sf = document.querySelector('#sf')
var sq = document.querySelector('#sq')
var xc = document.querySelector('#xc')
var http = new XMLHttpRequest();
http.open('GET', 'http://127.0.0.1:8000/get_area', false)
http.send()
var arr = JSON.parse(http.response).data
//省份
for (var i = 0; i < arr.length; i++) {
    var option = document.createElement('option')
    option.text = arr[i].name
    option.value = arr[i].id
    sf.appendChild(option)
}
//市区
sf.onchange = function () {
    sq.innerHTML = "<option value='0' disabled selected>请选择市区</option>"
    xc.innerHTML = "<option value='0' disabled selected>请选择县城</option>"
    sq_val = sf.value
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].id == sq_val) {
            for (var j = 0; j < arr[i].children.length; j++) {
                var option = document.createElement('option')
                option.text = arr[i].children[j].name;
                option.value = arr[i].children[j].id;
                sq.appendChild(option);

            }
        }
    }
}
//县城
sq.onchange = function () {
    xc.innerHTML = "<option value='0' disabled selected>请选择县城</option>"
    xc_val = sq.value
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr[i].children.length; j++) {
            if (arr[i].children[j].id == xc_val) {
                for (var l = 0; l < arr[i].children[j].children.length; l++) {
                    var option = document.createElement('option')
                    option.text = arr[i].children[j].children[l].name;
                    option.value = arr[i].children[j].children[l].id;
                    xc.appendChild(option);
                }
            }
        }
    }
}
// 添加修改个人信息
$('#set_yes').click(function () {
    var uname = $('.set_name > input').val()
    var iphone = $('.set_iphone > input').val()
    var address = $('.set_address > input').val()
    // console.log(uname,iphone,address);
    console.log(sf.value,sq.value,xc.value);
    console.log($('#mr')[0].checked);
})